package api_base

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"wrblog-api-go/app/common/token"
	"wrblog-api-go/app/model/model_base"
	"wrblog-api-go/app/service/service_base"
	"wrblog-api-go/pkg/request"
	"wrblog-api-go/pkg/result"
	"wrblog-api-go/pkg/status"
)

// @Tags  Base - 文章组管理
// @Summary  获取组列表
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param baseArticleGroup query model_base.SelectBaseArticleGroup true "baseArticleGroup"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticleGroup/{page} [get]
func ApiArticleGroupPageList(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	baseArticleGroup := raw.(model_base.SelectBaseArticleGroup)
	result.PageRes(service_base.GetArticleGroupPageList(baseArticleGroup)).Json(c)
}

// @Tags  Base - 文章组管理
// @Summary  获取当前用户的组列表
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param baseArticleGroup query model_base.SelectBaseArticleGroup true "baseArticleGroup"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticleGroup/myPage [get]
func ApiMyArticleGroupPageList(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	baseArticleGroup := raw.(model_base.SelectBaseArticleGroup)
	baseArticleGroup.UserId = token.GetUserId(c)
	result.PageRes(service_base.GetArticleGroupPageList(baseArticleGroup)).Json(c)
}

// @Tags  Base - 文章组管理
// @Summary  获取组详情
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param groupId path int true "groupId"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticleGroup/:groupId [get]
func ApiArticleGroupInfo(c *gin.Context) {
	groupId, err := strconv.Atoi(c.Param("groupId"))
	if err != nil {
		result.Wrap(status.BadRequest, "参数读取失败").Json(c)
	}
	result.Res(service_base.GetArticleGroupInfo(groupId)).Json(c)
}

// @Tags  Base - 文章组管理
// @Summary  添加组
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param baseArticleGroup body model_base.BaseArticleGroup true "baseArticleGroup"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticleGroup/add [post]
func ApiAddArticleGroup(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	baseArticleGroup := raw.(model_base.BaseArticleGroupPo)
	loginUser := token.GetLoginUser(c)
	baseArticleGroup.UserId = loginUser.UserId
	baseArticleGroup.CreateBy = loginUser.Uk
	result.Res(service_base.AddArticleGroup(baseArticleGroup)).Json(c)
}

// @Tags  Base - 文章组管理
// @Summary  修改组
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param baseArticleGroup body model_base.BaseArticleGroup true "baseArticleGroup"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticleGroup/edit [put]
func ApiEditArticleGroup(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	baseArticleGroup := raw.(model_base.BaseArticleGroupPo)
	baseArticleGroup.UpdateBy = token.GetUk(c)
	result.Res(service_base.EditArticleGroup(baseArticleGroup)).Json(c)
}

// @Tags  Base - 文章组管理
// @Summary  删除组（逻辑删除）
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param groupIds query string true "groupIds"
// @Success 200 {object} result.Result "OK"
// @Router /base/baseArticleGroup/removes [delete]
func ApiRemoveArticleGroup(c *gin.Context) {
	groupIds := c.Query("groupIds")
	result.Res(service_base.RemoveArticleGroup(groupIds, token.GetUk(c))).Json(c)
}
